//    Copyright (C) Kherty.  All rights reserved.
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class DisplayUnits : IOfficeXmlSerializable, INotifyPropertyChanged
    {
        private  Unit unit = new BuiltInUnit();
        public Unit Unit
        {
            get { return unit; }
            set { unit = value;
                notifyPropertyChanged("Unit"); }
        }

        private void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }


        private DisplayUnitsLabel _displayUnitsLabel;
        public  DisplayUnitsLabel Label
        {
            get { return _displayUnitsLabel; }
            set { _displayUnitsLabel = value;
                notifyPropertyChanged("Label"); }
        }
        public ExtensionElement Extension { get; set; }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("dispUnits"))
            {
                Unit = Unit.ReadOptionalFrom(c);
                Label = c.ReadOptionalElement<DisplayUnitsLabel>("dispUnitsLbl");
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("dispUnits"))
            {
                c.WriteOptionalElement(Unit);
                c.WriteOptionalElement(Label);
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}